/**
 * 测试用例导出接口测试用例
 *
 * Created by qinmengchao on 2020/06/18.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');
const benchmarks = [require('./../../../benchmark/generate-tests/self_designed')];

module.exports = describe('/GET project-versions/:id/tests.json', function () {
  const administrator = new Administrator();
  let project = {};
  const newUserIds = [];
  function url(versionId) {
    return `/project-versions/${versionId}/tests.json`;
  }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((res) => {
        project = res;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'get');
  common.should.be.tester(uri, url, 'get');

  // 正常发起请求
  benchmarks.forEach((benchmark) => {
    it(`it should EXPORT all tests of ${benchmark.name}`, function (done) {
      let projectVersion;
      project.createRandomVersion()
        .then((res) => {
          projectVersion = res;
          return projectVersion.getAllTests(benchmark);
        })
        .then(() => project.creator.login())
        .then(() => {
          const { accessToken } = project.creator.token;
          chai.request(uri)
            .get(url(projectVersion.versionId))
            .set('Authorization', `Bearer ${accessToken}`)
            .end((err, res) => {
              res.should.have.status(200);
              common.should.be.exportTestCaseMessage(res.body);
              done();
            });
        })
        .catch(done);
    });
  });

  // 不可导出他人项目的测试用例
  it('it should fail to export tests when the project is not yours', function (done) {
    let projectVersion;
    administrator.createRandomTester()
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((project) => project.createRandomVersion())
      .then((version) => {
        projectVersion = version;
        return project.creator.login();
      })
      .then(() => {
        chai.request(uri)
          .get(url(projectVersion.versionId))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 不可导出不存在的项目文件的测试用例
  it('it should fail to export tests when the project is not exist', function (done) {
    let projectId;
    let projectVersion;
    project.creator.createRandomProject()
      .then((project) => {
        projectId = project.projectId;
        return project.createRandomVersion();
      })
      .then((createdVersion) => {
        projectVersion = createdVersion;
        return project.creator.removeProject(projectId);
      })
      .then(() => project.creator.login())
      .then(() => {
        chai.request(uri)
          .get(url(projectVersion.versionId))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });
});
